package bdqn.com.cui.serviet;

import bdqn.com.cui.pojo.User;
import bdqn.com.cui.service.impl.UserserviceImpl;
import bdqn.com.cui.utl.LayData;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "Userserviet",urlPatterns = "/usercrud")
public class Userserviet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");//设置页面属性为  UTF-8
        req.setCharacterEncoding("UTF-8");

        String opr=req.getParameter("opr");
        UserserviceImpl userservice=new UserserviceImpl();
        User user=new User();
        if (opr.equals("denlu")){//登录
            String zhanghao=req.getParameter("zhanghao");
            String password=req.getParameter("password");
            User denlu = userservice.denlu(zhanghao, password);
            if (denlu.getQx().equals("2")){
               resp.sendRedirect("index.jsp");
           }else if (denlu.getType()==1){
               resp.getWriter().write("<script>alert('账号或密码错误！');location.href='denlu.jsp'</script>");
           }
        }

        if(opr.equals("set")){ //全查
            int current= Integer.parseInt(req.getParameter("page"));
            String limit=req.getParameter("limit");
            List<User> list= userservice.getsenect((current-1)*Integer.parseInt(limit), Integer.parseInt(limit));
            int count= userservice.getcount();
            LayData layData=new LayData();
            layData.setCode(0);
            layData.setMsg("");
            layData.setCount(count);
            layData.setData(list);
            String p_name=req.getParameter("p_name");
            if (p_name!=null&&p_name!=""){
                List<User> idset = userservice.getidserent(p_name);
                layData.setData(idset);
            }else if(p_name==null||p_name==""){
                List<User> list1= userservice.getsenect((current-1)*Integer.parseInt(limit), Integer.parseInt(limit));
                layData.setData(list1);
            }
            String jsonstring= JSONObject.toJSONString(layData);
            resp.getWriter().write(jsonstring);
        }else if (opr.equals("insert")){        //新增
            String username=req.getParameter("username");
            String zhanghao=req.getParameter("zhanghao");
            String password=req.getParameter("password");
            String sex=req.getParameter("sex");
            String modules=req.getParameter("qx");
           int add= userservice.add(new User(username,sex,modules,zhanghao,password));
            if (add>0){
                String s = JSONObject.toJSONString(add);
                resp.getWriter().write(s);
            }else {
                resp.getWriter().write("<script>alert('添加失败！');location.href='houtai_yonghu.jsp'</script>");
            }
        }else if (opr.equals("upt")){//            修改
                String username=req.getParameter("username");
                String zhanghao=req.getParameter("zhanghao");
                String password=req.getParameter("password");
                String sex=req.getParameter("sex");
                String modules=req.getParameter("qx");
                int i= userservice.update(new User(username,sex,modules,zhanghao,password,user.getId()));
                if (i>0){
                    String s=JSONObject.toJSONString(i);
                    resp.getWriter().write(s);
                }
        }else if (opr.equals("idset")){//          账号查询
            String zhanghao=req.getParameter("zhanghao");
            List<User> getidserent = userservice.getidserent(zhanghao);
            req.getSession().setAttribute("getidserent",getidserent);
            resp.sendRedirect("user_edit.jsp");

        }else if (opr.equals("det")){//删除
            String zhanghao=req.getParameter("zhanghao");
            int det = userservice.delete(zhanghao);
            boolean f = false;
            if (det>0){
                f=true;
            }else {
            }
            resp.getWriter().write(String.valueOf(f));
        }else if(opr.equals("typeupdate")){
            String zhanghao=req.getParameter("zhanghao");
            String i=req.getParameter("i");
            int typeupdate = userservice.Typeupdate(i,zhanghao);
            String s=JSONObject.toJSONString(typeupdate);
            resp.getWriter().write(s);
        }else if (opr.equals("getsex")){
         Map map= userservice.getsex();
       String jsonstring=JSONObject.toJSONString(map);
       resp.getWriter().write(jsonstring);
        }
    }
}
